package com.memory.monitor.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.constant.ExcelConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.job.AreaExcelListener;
import com.memory.monitor.job.DistributeExcelListener;
import com.memory.monitor.mapper.DistributeMapper;
import com.memory.monitor.model.dto.distribute.DistributeAddRequest;
import com.memory.monitor.model.dto.distribute.DistributeQueryRequest;
import com.memory.monitor.model.dto.distribute.DistributeUpdateRequest;

import com.memory.monitor.model.entity.Area;
import com.memory.monitor.model.entity.Distribute;

import com.memory.monitor.model.entity.User;
import com.memory.monitor.model.vo.AreaVO;
import com.memory.monitor.model.vo.DistributeVO;
import com.memory.monitor.service.DistributeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.memory.monitor.constant.ExcelConstant.*;

/**
* @author 21731
* @description 针对表【distribute( 产区分布)】的数据库操作Service实现
* @createDate 2024-09-02 09:59:36
*/
@Service
public class DistributeServiceImpl extends ServiceImpl<DistributeMapper, Distribute> implements DistributeService {

    /**
     * 封装DistributeVO对象
     * @param distribute
     * @return
     */
    @Override
    public DistributeVO getdistributeVO(Distribute distribute) {

        if (distribute == null) {
            return null;
        }
        DistributeVO distributeVO = new DistributeVO();
        BeanUtils.copyProperties(distribute, distributeVO);
        return distributeVO;
    }

    /**
     * 封装distributeVO对象
     * @param distributeList
     * @return
     */
    @Override
    public List<DistributeVO> getdistributeVO(List<Distribute> distributeList) {
        if (CollectionUtils.isEmpty(distributeList)) {
            return new ArrayList<>();
        }
        return distributeList.stream().map(this::getdistributeVO).collect(Collectors.toList());
    }

    /**
     * 查找产区分布信息
     * @param distributeQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Distribute> getQueryWrapper(DistributeQueryRequest distributeQueryRequest) {
        if (distributeQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = distributeQueryRequest.getId();

        String productType = distributeQueryRequest.getProductType();
        String productName = distributeQueryRequest.getProductName();

        QueryWrapper<Distribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(productType), "product_type", productType);
        queryWrapper.eq(StringUtils.isNotBlank(productName), "product_name", productName);
        return queryWrapper;
    }

    /**
     * 添加产区分布信息
     * @param distributeAddRequest
     * @return
     */
    @Override
    public Long addDistribute(DistributeAddRequest distributeAddRequest) {
        if (distributeAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Distribute distribute = new Distribute();

        BeanUtils.copyProperties(distributeAddRequest, distribute);
        boolean result = this.save(distribute);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return distribute.getId();
    }

    /**
     * 修改产区分布信息
     * @param distributeUpdateRequest
     * @return
     */
    @Override
    public Boolean updateDistribute(DistributeUpdateRequest distributeUpdateRequest) {
        if (distributeUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Distribute distribute =new Distribute();

        BeanUtils.copyProperties(distributeUpdateRequest,distribute);
        boolean result = this.updateById(distribute);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除产区分布信息
     * @param deleteRequest
     * @return
     */
    @Override
    public Boolean deleteDistribute(DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = this.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    @Override
    public List<DistributeVO> listDistributeByPage() {
        return getdistributeVO(this.list());
    }

    @Override
    public Boolean readExcel(MultipartFile multipartFile) {
        // 1.转换文件为字节流
        InputStream inputStream;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 2.执行文件处理逻辑
        EasyExcel.read(inputStream, Distribute.class, new DistributeExcelListener()).sheet().doRead();
        // 3.入库
        boolean saveBatch = this.saveBatch(DistributeExcelListener.CACHED_DATA_LIST);

        ThrowUtils.throwIf(!saveBatch, ErrorCode.PARAMS_ERROR);
        return true;
    }

    @Override
    public Boolean writeExcel(String path) {
        // 1.定义文件路径
        String fullPath = DOWNLOAD_PATH + AREA_TABLE_NAME;
        // 2.检查文件是否存在
        ExcelConstant.isExist(fullPath);
        // 3.根据路径导出
        List<DistributeVO> distributeList = this.listDistributeByPage();
        EasyExcel.write(fullPath, User.class).sheet(AREA_SHEET_NAME).doWrite(distributeList);

        return true;
    }
}




